﻿function CreateUser() {
    if (ValidateControls() == true) {
        response = SubmitUserCreateForm();
        if (response.indexOf("Success") > -1) {
            alert("New user was successfuly created");
            DoCleanUp();
        } else {
            document.getElementById('lblErrorMessage').innerHTML = response;
        }
    }
}

function SubmitUserCreateForm() {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var url = "Administration/AddUser.aspx";
    url = url + "?createuser=1";
    url = url + "&username=" + URLEncode(document.getElementById('txtUserName').value);
    url = url + "&password=" + URLEncode(document.getElementById('txtPassword').value);
    url = url + "&email=" + URLEncode(document.getElementById('txtEmail').value);
    url = url + "&secretquestion=" + URLEncode(document.getElementById('ddSecretQuestion').value);
    url = url + "&secretanswer=" + URLEncode(document.getElementById('txtAnswer').value);
    url = url + "&name=" + URLEncode(document.getElementById('txtName').value);
    url = url + "&address=" + URLEncode(document.getElementById('txtAddress').value);
    url = url + "&age=" + URLEncode(document.getElementById('txtAge').value);
    url = url + "&department=" + URLEncode(document.getElementById('txtDepartment').value);
    url = url + "&contactno=" + URLEncode(document.getElementById('txtPrimaryContactNumber').value);
    url = url + "&usertype=" + URLEncode(document.getElementById('ddlType').value);
    url = url + "&sid=" + Math.random();


    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
    response = xmlhttp.responseText;
    //alert(response);
    response = response.substring(response.indexOf("<response>"), response.indexOf("</response"));
    //alert(response);

    return response;
}

function ValidateControls() {
    var errMsg = '';

    if (document.getElementById('txtUserName').value == "") {
        errMsg += 'User Name is Required<br/>';
    }

    if (document.getElementById('txtPassword').value == "") {
        errMsg += 'Password is Required<br/>';

    } else {
        var password = document.getElementById('txtPassword').value;
        if (password.length < 7) {
            errMsg += 'Password should be at least 7 characters<br/>';
        }
    }

    if (document.getElementById('txtEmail').value == "") {
        errMsg += 'Email is Required<br/>';
    }

    if (document.getElementById('ddSecretQuestion').value == "") {
        errMsg += 'Security Question is Required<br/>';
    }

    if (document.getElementById('txtAnswer').value == "") {
        errMsg += 'Security Answer is Required<br/>';
    }

    if (document.getElementById('txtConfirmPassword').value != document.getElementById('txtPassword').value) {
        errMsg += 'Password and Confirm Password Should Match<br/>';
    }


    if (document.getElementById('txtAge').value != "") {
        if (isNaN(document.getElementById('txtAge').value)) {
            errMsg += 'Age should be a number<br/>';
        }
    } else {
        errMsg += 'Age is required<br/>';
    }
    
    if (document.getElementById('txtName').value == "") {
        errMsg += 'Name is Required<br/>';
    }

    document.getElementById('lblErrorMessage').innerHTML = errMsg;

    if (errMsg == '') {
        return true;
    } else {
        return false;
    }
}


function DoCleanUp() {
    document.getElementById('txtUserName').value = "";

    document.getElementById('txtPassword').value = "";

    document.getElementById('txtEmail').value = ""

    document.getElementById('ddSecretQuestion').value = 0;

    document.getElementById('txtAnswer').value = ""

    document.getElementById('txtConfirmPassword').value = "";

    document.getElementById('txtName').value = "";
    
    document.getElementById('txtAddress').value = "";

    document.getElementById('txtAge').value = "";
    
    document.getElementById('txtDepartment').value = "";

    document.getElementById('txtPrimaryContactNumber').value = "";

    document.getElementById('ddlType').value = 0;
}